<template>
    <form>
        <slot />
    </form>
</template>
<script setup>
import { provide, onMounted } from "vue";

const emits = defineEmits(["update:context"]);

const props = defineProps({
  model: {
    type: Object,
    default: () => ({}),
  },
});

const FormContext = {
  model: props.model,
  items: [],
  register: (item) => {
    FormContext.items.push(item);
  },
  validate: (cb) => {
    let flag = true, failList = [];
    FormContext.items.forEach(item => {
      if (!item.validate()) {
        flag = false;
        failList.push(item.name);
      }
    });
    cb(flag, failList);
  },
};
provide("FormContext", FormContext);
onMounted(() => {
  emits("update:context", FormContext);
});
</script>